$(function(){
    var editTree = {
        allbm:$("#cykm_allbm").tree({
            url:$.webPath() + '/sys/wzbm/get_treejson',
            checkbox:true,
            onExpand:function(node){
                $(node.target).next("ul").find("li").each(function(){
                    var div = $(this).find(".tree-node");
                    var id = div.attr("node-id");
                    var bgNode = editTree.allbm.tree("find","'"+id+"'");
                    if(bgNode.attributes.bgCode){
                        div.attr("title","保供物资编码("+bgNode.attributes.bgCode+")");
                    }
                });
            },
            onLoadSuccess:function(node,data){
                if(!node){
                    node = $(this).tree("getRoot");
                    $(node.target).next("ul").find("li").each(function(){
                        var div = $(this).find(".tree-node");
                        var id = div.attr("node-id");
                        var bgNode = editTree.allbm.tree("find","'"+id+"'");
                        if(bgNode.attributes.bgCode){
                            div.attr("title","保供物资编码("+bgNode.attributes.bgCode+")");
                        }
                    });
                }
            }
        }),
        editbm:$("#cykm_editbm").tree({
            checkbox:true,
//            data:[{id: "0:0", text: "编码物资", attributes: {"level": "0", "children": "false"}, state: "open",children:[]}],
            url:$.webPath() + '/sys/wzbm/get_treejsonbycodeid?codeId='+$("#cykm_codeid").val(),
            onContextMenu: function(e,node){
                e.preventDefault();
                $(this).tree('select',node.target);
                $('#cykm-bm-mm').menu('show',{
                    left: e.pageX,
                    top: e.pageY
                });
            },
            loadFilter: function (rows) {
                rows.push({id: "0:0", text: "编码物资", state: "open"});
                return convert(rows);
            }
        }),
        appendToTree:function(node){
            var opt = editTree.editbm.tree("options");
            opt.url = $.webPath() + '/sys/wzbm/get_treejson';
            opt.loadFilter = function(rows){
                return rows;
            }
            var parent = editTree.allbm.tree("getParent",node.target);
            if(!parent)
                return;
            var editParent = editTree.editbm.tree("find","'"+parent.id+"'");
            if(!editParent){
                editTree.appendToTree(parent);
                editParent = editTree.editbm.tree("find","'"+parent.id+"'");
            }
            var editNode = editTree.editbm.tree("find","'"+node.id+"'");
            if(!editNode){
                editTree.editbm.tree("append",{
                    parent:editParent.target,
                    data:[{
                        id:node.id,
                        text:node.text,
//                        attributes:node.attributes,
                        state:node.state,
//                        parentId:editParent.id,
                        children:node.children
                    }]
                });
            }
        },
        dropSelect:function(node){
            var parent = editTree.editbm.tree("getParent",node.target);
            if(!parent)
                return;
            node = editTree.editbm.tree("find","'"+node.id+"'");
            if(node)
                editTree.editbm.tree("remove",node.target);
        }
    };

    $("#cykm-bm-select").click(function(){
        var checked = editTree.allbm.tree("getChecked");
        for(var i=0;i<checked.length;i++){
            editTree.appendToTree(checked[i])
        }
    });
    $("#cykm-bm-cancel").click(function(){
        var checked = editTree.editbm.tree("getChecked");
        for(var i=0;i<checked.length;i++){
            editTree.dropSelect(checked[i])
        }
    });
    $("#cykm-bm-save").click(function(){
        var root = editTree.editbm.tree("getRoot");
        var data = editTree.editbm.tree("getChildren",root.target);
        cykmbmszwin.mask();
        $("#cykm-bmfw-save").form("submit",{
            onSubmit: function (param) {
                if(data.length == 0){
                    cykmbmszwin.mask("hide");
                    $.messager.alert("警告","未选中任何物资编码","warning");
                    return false;
                }
                for(var i=0;i<data.length;i++){
                    var children = editTree.editbm.tree("getChildren",data[i].target).length;
                    if(children != 0){
                        continue;
                    }
//                    alert(data[i].id);
                    param["bmList["+i+"]"] = data[i].id;
                }
            },
            success:function(data){
                var obj = eval('(' + data + ')');
//                alert(obj.statusCode);
                cykmbmszwin.mask("hide");
                if(obj.statusCode == 200){
                    $.messager.show({title:'提示',msg:"保存成功",showType:'slide'});
                    cykmbmszwin.window("close");
                } else {
                    $.messager.alert("info",data.message);
                }
            }
        });
    });
    $("#cykm-bm-mm .remove").click(function(){
        var node = editTree.editbm.tree('getSelected');
        var parent = editTree.editbm.tree('getParent',node.target);
        if(!parent){
            var chiledren = editTree.editbm.tree('getChildren',node.target);
            for(var i=0;i<chiledren.length;i++){
                editTree.editbm.tree('remove', chiledren[i].target);
            }
            return;
        }
        editTree.editbm.tree('remove', node.target);
    });
    $("#cykm-bm-mm .expand").click(function(){
        var node = editTree.editbm.tree('getSelected');
        editTree.editbm.tree('expand', node.target);
    });
    $("#cykm-bm-mm .collapse").click(function(){
        var node = editTree.editbm.tree('getSelected');
        editTree.editbm.tree('collapse', node.target);
    });
});